package yxk.operation;

import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.args.AfterOperationArgs;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.openapi.common.result.CustomApiResult;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.wtc.wtis.webapi.punchcard.PunchCardApi;
import kd.wtc.wtis.webapi.punchcard.model.PunchCardReqMode;
import kd.wtc.wtis.webapi.punchcard.model.PunchCardRespModel;
import kd.wtc.wtis.webapi.punchcard.validate.ListValidator;
import yxk.task.utils.PunchcardDataUtil;
import yxk.utils.BosUtil;
import yxk.utils.DateUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 *  调用打卡数据
 */
public class CardRetrievalDataOperation extends AbstractOperationServicePlugIn {
    private static final Log LOG = LogFactory.getLog(CardRetrievalDataOperation.class);
    @Override
    public void afterExecuteOperationTransaction(AfterOperationArgs e) {
        PunchcardDataUtil punchcardDataUtil=new PunchcardDataUtil();

        SimpleDateFormat format=new SimpleDateFormat("yyyyMMdd");
        DynamicObject[] entities = e.getDataEntities();
        for (DynamicObject row : entities) {
            long beginTime = System.currentTimeMillis();
            //获取单据编码
           /* String number = BosUtil.getNumber(row);
            row.set("number",number);*/
            //判断同步那边系统数据
            String mulcombofield = row.getString("nwg5_mulcombofield");
            //开始时间、结束时间
            Date begindate = row.getDate("nwg5_begindate");
            Date enddate = row.getDate("nwg5_enddate");
            try {
                if (mulcombofield.contains("HK")) {
                    String endStr = DateUtil.getISO8601TimestampFromDateStr(enddate);
                    String startStr = DateUtil.getISO8601TimestampFromDateStr(begindate);
                    String receiveEndTime = DateUtil.getISO8601TimestampFromDateStr(new Date());
                    LOG.info("CardRetrievalDataOperation.execute.nwg5_start:{}，nwg5_end：{}", begindate, enddate);
                    List<ListValidator<PunchCardReqMode>> haikanPunchcardObtainList = punchcardDataUtil.getHAIKANPunchcardObtainList(startStr, endStr, startStr, receiveEndTime);
                    executeCardRecordService(haikanPunchcardObtainList);
                    LOG.info("CardRetrievalDataOperation.execute.haikanPunchcardObtainList:{}", haikanPunchcardObtainList);
                }
                if (mulcombofield.contains("FS")) {
                    String begin = format.format(begindate);
                    String end = format.format(enddate);
                    List<ListValidator<PunchCardReqMode>> haikanPunchcardObtainList = punchcardDataUtil.getFSPunchcardObtainList(begin, end);
                    executeCardRecordService(haikanPunchcardObtainList);
                }
                long endTime = System.currentTimeMillis();
                long cost = endTime - beginTime;
                row.set("nwg5_cost",cost);
                row.set("nwg5_tietaskstatus",0);

            }catch (Exception exception){
                row.set("nwg5_tietaskstatus",1);
                row.set("nwg5_largetextfield",exception);
                exception.printStackTrace();
                LOG.info("CardRetrievalDataOperation.execute.exception:{}", exception);
            }
            SaveServiceHelper.saveOperate(row.getDynamicObjectType().getName(),new DynamicObject[]{row}, OperateOption.create());
        }
    }

    private void executeCardRecordService(List<ListValidator<PunchCardReqMode>> haikanPunchcardObtainList){
        if (haikanPunchcardObtainList != null) {
            PunchCardApi api=new PunchCardApi();
            for (ListValidator<PunchCardReqMode> punchCardReqModes : haikanPunchcardObtainList) {
                LOG.info("CardRetrievalDataOperation.execute.punchCardRespModel:{}", punchCardReqModes);
                try{
                    CustomApiResult<PunchCardRespModel> punchCardRespModel = api.syncCardRecordService(punchCardReqModes);
                    LOG.info("CardRetrievalDataOperation.execute.punchCardRespModel:{}", punchCardRespModel);
                }catch (Exception e) {
                    e.printStackTrace();
                    LOG.info("CardRetrievalDataOperation.execute.exception:{}", e);
                }
            }
        }
    }
}
